//
//  KnowledgeDetailViewController.m
//  yilutongxing
//
//  Created by ios-21 on 17/4/10.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "KnowledgeDetailViewController.h"
#import "YYText.h"
#import "YZEModel.h"

#define LeftItemLength ((63/600.0)*self.view.frame.size.width)
#define RightItemLength ((87/600.0)*self.view.frame.size.width)

#define View_X 20
#define View_Y 30
#define LineSpacing 9
#define TitleLableFont 17

#define ClickUrl @"/api/transplant-knowledges/"
#define URL_Favoriate @"/api/lore-collections"

@interface KnowledgeDetailViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIView *underNavigationView;

@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;

@property (nonatomic,strong)YYTextView *myYYtextView;
@property (nonatomic,strong)UILabel *titleLable;
@property (nonatomic,strong)UILabel *dateLable;

@property (nonatomic,strong)YZEModel *dataModel;
@property (nonatomic,copy)NSString *ID;
@end

@implementation KnowledgeDetailViewController
-(void)getWebID:(NSString *)ID{
      self.ID = ID;
}
-(void)setScrollViewFrame{
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    CGFloat navh = rectStatus.size.height + rectNav.size.height;
    self.myScrollView.frame = CGRectMake(0, navh, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - navh);
    
}
-(void)creatControls{
    self.titleLable = [UILabel new];
    self.dateLable = [UILabel new];
    self.myYYtextView = [YYTextView new];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y <= 0) {
        CGPoint conOffset = scrollView.contentOffset;
        conOffset.y = 0;
        scrollView.contentOffset = conOffset;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myScrollView.delegate = self;
    [self creatControls];
    [self setScrollViewFrame];
    self.myScrollView.showsVerticalScrollIndicator = NO;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackGroundImage"] forBarMetrics:UIBarMetricsDefault];
    [[YZENetwork sharedInstace] getRequestWithUrl:[NSString stringWithFormat:@"%@%@%@",BASE,ClickUrl,self.ID] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        
        self.dataModel = [YZEModel mj_objectWithKeyValuesAdd:responseObject];
        [self setClickViewContent];
    }];
    
//导航栏titleview
   
    [self setTitleViewAlignment];
    
    [self setClickViewContent];
}
-(void)setClickViewContent{
    
     self.title =self.dataModel.name;
    //导航栏下方的view
    self.underNavigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 15)];
    self.underNavigationView.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
    [self.myScrollView addSubview:self.underNavigationView];
    //显示内容
    //titleLable
    self.titleLable.numberOfLines = 0;
    self.titleLable.frame =CGRectMake(View_X, self.underNavigationView.frame.origin.y+self.underNavigationView.frame.size.height+15, [UIScreen mainScreen].bounds.size.width-(View_X*2),1000);
    NSString *str = self.dataModel.name;
    self.titleLable.text = str;
    //计算行数
    CGFloat labelHeight = [self.titleLable sizeThatFits:CGSizeMake(self.titleLable.frame.size.width, MAXFLOAT)].height;
    NSNumber *count = @((labelHeight) / self.titleLable.font.lineHeight);
    if ([count integerValue]  > 1 ) {//通过行数进行判断
        NSAttributedString *s = [self setLineSpacingWithControl:nil AndString:str AndLineSpacing:LineSpacing AndFont:TitleLableFont];
        self.titleLable.attributedText = s;
    }
    [self.titleLable sizeToFit];
    
    [self.myScrollView addSubview:self.titleLable];
    //dateLable
    self.dateLable.frame = CGRectMake(View_X, self.titleLable.frame.origin.y+self.titleLable.frame.size.height+9, [UIScreen mainScreen].bounds.size.width-(View_X*2), 15);
    self.dateLable.font = [UIFont systemFontOfSize:12];
    self.dateLable.text = self.dataModel.createdDate;
    [self.myScrollView addSubview:self.dateLable];
    
    //YYtext
    self.myYYtextView.showsVerticalScrollIndicator = NO;
    self.myYYtextView.editable = NO;
    NSString *tvStr =self.dataModel.content;
    //获取高度
    CGFloat textViewHight = [self heightForString:tvStr fontSize:15 andWidth:[UIScreen mainScreen].bounds.size.width-(View_X*2)];
    self.myYYtextView.frame = CGRectMake(View_X, self.dateLable.frame.origin.y+self.dateLable.frame.size.height+9, [UIScreen mainScreen].bounds.size.width-(View_X*2),textViewHight);
    self.myYYtextView.attributedText = [self setLineSpacingWithControl:nil AndString:tvStr AndLineSpacing:8 AndFont:15];
    
    [self.myYYtextView sizeToFit];
    
    [self.myScrollView addSubview:self.myYYtextView];
    
    //设置scrollViewContentSize
    self.myScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, self.myYYtextView.frame.origin.y+self.myYYtextView.frame.size.height + 100);
}
#pragma 字体行间距
-(NSAttributedString *)setLineSpacingWithControl:(UITextView*)textView AndString:(NSString *)string AndLineSpacing:(CGFloat)lineSpacing AndFont:(CGFloat)font{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;// 字体的行间距
//    NSDictionary *attributes = @{
//                                 NSFontAttributeName:[UIFont systemFontOfSize:font],
//                                 NSParagraphStyleAttributeName:paragraphStyle
//                                 };
    //return [[NSAttributedString alloc] initWithString:string attributes:attributes];
    NSAttributedString *attrstr = [[NSAttributedString alloc] initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding]
                                                                   options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
                                                        documentAttributes:nil error:nil];
    return attrstr;
}
#pragma 自适应高度
- (float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width{
    UITextView *detailTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, width, 0)];
    detailTextView.font = [UIFont systemFontOfSize:fontSize];
    detailTextView.text = value;
    CGSize deSize =[detailTextView sizeThatFits:CGSizeMake(width,CGFLOAT_MAX)];
    return deSize.height;
}
-(void)setTitleViewAlignment{
    //设置右边的Item
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, RightItemLength , 20)];
//    view.backgroundColor = [UIColor greenColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
     self.navigationItem.rightBarButtonItems=@[[[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collection)],[[UIBarButtonItem alloc]initWithCustomView:view]];
    //设置左边的Item
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, LeftItemLength, 20)];
//    view1.backgroundColor=[UIColor blueColor];
    self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)],[[UIBarButtonItem alloc]initWithCustomView:view1]];
}
//收藏
-(void)collection{
    
    NSDictionary *dic2=@{@"loreId":self.ID};
    [[YZENetwork sharedInstace]postRequestWithUrl:[BASE stringByAppendingString:URL_Favoriate] parameters:dic2 delegate:self requestTag:1 successBlock:^(id responseObject) {
        [UIView showToastWindowWithMessage:@"收藏成功"];
    }];
    
}
- (void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
